package com.mycompany.inventory.persistence;

import com.mycompany.common.AbstractDao;
import com.mycompany.common.jdbc.ConnectionDatabase;
import com.mycompany.inventory.pojo.PaymentTerm;

import java.util.ArrayList;
import java.util.List;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by My Company.
 * User: loitran
 * Date: Feb 29, 2012
 * Time: 1:53:19 PM
 * com.mycompany.inventory.persistence.
 */
public class PaymentTermDAO extends AbstractDao<PaymentTerm>{
	
	private Connection con = this.connect;
	public static final String SELECT_ALL_SQL = "select * from oce_payment_term";
	public static final String SELECT_SQL = "select * from oce_payment_term where paymentTermId = ?";
	
	public PaymentTerm create(PaymentTerm obj){
		try{
			
		}catch (Exception e) {
			// TODO: handle exception
		}
		return obj;
	}
	
	public PaymentTerm update(PaymentTerm obj){
		try{
			
		}catch (Exception e) {
			// TODO: handle exception
		}
		return obj;
	}
	
	public PaymentTerm find(long id){
		List<PaymentTerm> payments = new ArrayList<PaymentTerm>();
		try{
			PreparedStatement ps = con.prepareStatement(SELECT_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setLong(1,id);
			payments = convert(ConnectionDatabase.executeQuery(ps));
		}catch (Exception e) {
			// TODO: handle exception			
		}
		return payments.size() == 0 ? null : payments.get(0);
	}
	
	public List<PaymentTerm> findAll(){
		List<PaymentTerm> payments = new ArrayList<PaymentTerm>();
		try{
			payments = convert(ConnectionDatabase.executeQuery(con.prepareStatement(SELECT_ALL_SQL, PreparedStatement.RETURN_GENERATED_KEYS)));
		}catch (Exception e) {
			// TODO: handle exception
			payments = null;
		}
		return payments;
	}
	
	public void delete(PaymentTerm obj){
		try{
			
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	private List<PaymentTerm> convert(ResultSet rs) throws SQLException {
		List<PaymentTerm> payments = new ArrayList<PaymentTerm>();
		while (rs.next()) {
	        // Get the data from the row using the column index
	        String id = rs.getString(1);
	        String name = rs.getString(2);
	        String remark = rs.getString(3);
	        payments.add(new PaymentTerm(id, name, remark, 0));
	    }
		rs.close();
		return payments;
	}
}
